//
//  MyViewController.m
//  Daybook
//
//  Created by liutaifeng on 16/5/23.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "MyViewController.h"
#import "Common.h"
#import <BmobSDK/Bmob.h>

@interface MyViewController ()
<
 MyViewDelegate,
UIImagePickerControllerDelegate,
UINavigationControllerDelegate,
UIActionSheetDelegate
>
@property (nonatomic, strong) MyView *myView;
@end

@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationController.navigationBar.translucent = NO;
    
    [self createView];
}
- (void)createView {
    self.myView = [[MyView alloc] initWithFrame:self.view.frame];
    _myView.delegate = self;
    [self.view addSubview:_myView];
}
- (void)backAction {
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
//    CATransition *backPageAnimation = [CATransition animation];
//    backPageAnimation.duration = 0.4;
//    backPageAnimation.type = kCATransitionMoveIn;
//    backPageAnimation.subtype = kCATransitionFromBottom;
//    
//    [self.navigationController.view.layer addAnimation:backPageAnimation forKey:nil];
//    
//    [self.navigationController popViewControllerAnimated:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)passwordAction {
    PasswordViewController *passwordVC = [PasswordViewController new];
//    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:passwordVC];
//    
//    [self presentModalViewController:navi animated:YES];
    [self presentViewController:passwordVC animated:YES completion:^{
        
        
    }];
}

- (void)logout {
    [BmobUser logout];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
//    CATransition *backPageAnimation = [CATransition animation];
//    backPageAnimation.duration = 0.4;
//    backPageAnimation.type = kCATransitionMoveIn;
//    backPageAnimation.subtype = kCATransitionFromBottom;
//    
//    [self.navigationController.view.layer addAnimation:backPageAnimation forKey:nil];
//    
//    [self.navigationController popViewControllerAnimated:NO];
}

- (void)deleteDataAction {
    DeleteDataViewController *deleteVC = [DeleteDataViewController new];
    [self presentViewController:deleteVC animated:YES completion:^{
        
        
    }];
}

- (void)clickHeaderImage {
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"从相册中选取图片" otherButtonTitles:nil, nil];
    
    
    
    [actionSheet showInView:self.view];
    
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"我被摸了");
    
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.allowsEditing = YES;
    
    picker.delegate = self;
    
    
    [self presentViewController:picker animated:YES completion:^{
        
        
    }];
    
}
//选择图片的协议方法,点choose时被触发.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"%@",info);
    //模态消失
    [picker dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    //把图片加载在imageView上
    UIImage *image = info[UIImagePickerControllerOriginalImage];
    self.myView.headImageView.image = image;
    
    NSString *sandBox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *docPath = [sandBox stringByAppendingPathComponent:@"xiaoming.png"];
    
    
    [UIImagePNGRepresentation(image) writeToFile:docPath atomically:YES];
    
    
    
    //        NSBundle    *bundle = [NSBundle mainBundle];
    //        NSString *fileString = [NSString stringWithFormat:@"%@/xiaoming.png" ,[bundle bundlePath] ];
    //    BmobObject *obj = [[BmobObject alloc] initWithClassName:@"_user"];
    //    BmobFile *file1 = [[BmobFile alloc] initWithFileName:@"xiaoming" withFileData:UIImagePNGRepresentation(image)];
    //    [file1 saveInBackground:^(BOOL isSuccessful, NSError *error) {
    //        //如果文件保存成功，则把文件添加到filetype列
    //        if (isSuccessful) {
    //            [obj setObject:file1  forKey:@"image"];
    //            [obj saveInBackground];
    //            //打印file文件的url地址
    //            NSLog(@"file1 url %@",file1.url);
    //        }else{
    //            //进行处理
    //        }
    //    }];
    
    
    
    BmobUser *bUser = [BmobUser getCurrentObject];
    if (bUser) {
        //进行操作
        BmobFile *file1 = [[BmobFile alloc] initWithFileName:@"xiaoming.png" withFileData:UIImagePNGRepresentation(image)];
        [bUser setObject:file1 forKey:@"image"];
        [file1 saveInBackground:^(BOOL isSuccessful, NSError *error) {
            //如果文件保存成功，则把文件添加到filetype列
            if (isSuccessful) {
                [bUser setObject:file1  forKey:@"image"];
                [bUser updateInBackground];
                //打印file文件的url地址
                NSLog(@"file1 url %@",file1.url);
                [[NSNotificationCenter defaultCenter] postNotificationName:@"headChanged" object:image];
            }else{
                //进行处理
            }
        }];
        
        
        
        
    }else{
        //对象为空时，可打开用户注册界面
    }
    
    
    
    
}

- (void)sendToAboutUs {
    AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc]init];
    
    [self presentViewController:aboutUsVC animated:YES completion:^{
        
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
